package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC72 {

    public int minDistance(String s1, String s2) {
        int m = s1.length(), n = s2.length();
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i < m + 1; i++) {
            dp[i][0] = i;
        }

        for (int i = 1; i < n + 1; i++) {
            dp[0][i] = i;
        }

        for (int i = 1; i < m + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = getTripleMin(
                            dp[i - 1][j] + 1,   //i step, delete method
                            dp[i][j - 1] + 1,   //j step, insert method
                            dp[i - 1][j - 1] + 1  // both step, replace method
                    );
                }
            }
        }

        return dp[m][n];
    }

    public int getTripleMin(int a, int b, int c) {
        return Math.min(a, Math.min(b, c));
    }

    public static void main(String[] args) {
        LC72 an = new LC72();
        System.out.println(an.minDistance("horse", "ros"));
    }
}
